<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入文章作者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop"  clearable style="width: 200px">
          <el-option
              v-for="dict in article_is_top"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="文章状态" prop="status">
        <el-select v-model="queryParams.status"  clearable style="width: 200px">
          <el-option
              v-for="dict in article_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="发布时间" prop="sendTime">
        <el-date-picker clearable
          v-model="queryParams.sendTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择文章发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
<!--        <el-button icon="Refresh" @click="pythonArticle">爬取数据</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['article:article:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['article:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['article:article:remove']"
        >删除</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['article:article:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="编号"  width="55" align="center" prop="id" />
      <el-table-column label="文章标题" align="center" prop="title" />
      <el-table-column label="文章内容" width="120" align="center" >
        <template #default="scope">
          <el-button type="success" round @click="jumpArticle(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="文章字数"  align="center" prop="wordNum" />
      <el-table-column label="文章作者" align="center" prop="author" />
      <el-table-column label="点赞数量" align="center" prop="likeNum" />
      <el-table-column label="评论数量" align="center" prop="evaluate" />
      <el-table-column label="浏览数量" align="center" prop="see" />
      <el-table-column prop="isTop" label="是否置顶" width="80" align="center">
        <template #default="scope">
          <dict-tag :options="article_is_top" :value="scope.row.isTop" />
        </template>
      </el-table-column>

      <el-table-column label="文章照片" align="center" prop="img" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="发布时间" align="center" prop="sendTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>


      <el-table-column prop="status" label="文章状态" width="80" align="center">
        <template #default="scope">
          <dict-tag :options="article_status" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column prop="articleLabel" label="文章标签" width="90" align="center">
        <template #default="scope">
          <dict-tag  :options="articleLabelList" :value="scope.row.articleLabel" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" >
        <template #default="scope">
          <el-button v-if="scope.row.status === '1'" class="my-button" link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['article:article:edit']">修改</el-button>
          <el-button class="my-button" link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['article:article:remove']">删除</el-button>
          <el-button link type="primary" icon="Pointer" plain @click="examineArticle(scope.row)" v-if="scope.row.status==='1'" >审核</el-button>
<!--          <el-button link type="primary" icon="Pointer" @click="handlePublish(scope.row)" v-if="scope.row.sendTime==null" >发布</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" v-model="open" width="550px" append-to-body>
      <el-form ref="articleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>

        <el-form-item label="文章标签">
          <el-select v-model="form.labelIds" multiple placeholder="请选择">
            <el-option
                v-for="item in labels"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.isStart == 1"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否置顶" prop="isTop">
          <el-select v-model="form.isTop"  clearable >
            <el-option
                v-for="dict in article_is_top"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="文章照片" prop="img">
          <image-upload v-model="form.img" limit="1"/>
          <el-link href="https://www.gaoding.com/" style="margin-top: -40px" type="success" target="_blank">制作图片(宽:高=16:9)</el-link>
        </el-form-item>
      </el-form>


      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
<!--审核弹框-->
    <el-dialog v-model="centerIsible"  width="500" center>
<!--    <span>
      It should be noted that the content will not be aligned in center by
      default
    </span>-->
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="isBack(false)">打回</el-button>
          <el-button type="primary" @click="isBack(true)">
            通过
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Article">
import router from '@/router'
import {
  listArticle,
  getArticle,
  delArticle,
  addArticle,
  updateArticle,
  listAnyArticle,
  listArticlePython
} from "@/api/article/article";
import moment from 'moment';
import {parseTime} from "../../../utils/ruoyi.js";
import { ElMessage, ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance();
const { article_is_top,article_status,article_label } = proxy.useDict("article_is_top","article_status","article_label");
const articleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const labels = ref([]);
const articleLabelList = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    content: null,
    wordNum: null,
    authorId: null,
    author: null,
    likeNum: null,
    evaluate: null,
    see: null,
    isTop: null,
    img: null,
    sendTime: null,
    articleLabel:null,
    status: null,
  },
  rules: {
  },

});
const centerIsible = ref(false)
const { queryParams, form, rules } = toRefs(data);
const rowItem = ref()
function examineArticle(row){
  centerIsible.value = true
  rowItem.value = row
}

function isBack(flag){
  //通过
  if (flag){
    rowItem.value.sendTime = moment().format('YYYY-MM-DD HH:mm:ss');
    rowItem.value.status = '0'//文章表中的字段已发布
    rowItem.value.articleStatus = '0'//标签表中的数据
    updateArticle(rowItem.value).then(response => {
      getList();
      proxy.$modal.msgSuccess("发布成功");
    });
  }else {
    //未通过
    //rowItem.value.sendTime = moment().format('YYYY-MM-DD HH:mm:ss');
    rowItem.value.status = '2'//文章表中的字段已打回
    rowItem.value.articleStatus = '2'//标签表中的数据
    updateArticle(rowItem.value).then(response => {
      getList();
      proxy.$modal.msgSuccess("打回成功");
    });
  }
  centerIsible.value = false
  console.log(flag)
}
/**
 * 跳转文章详情
 */
function jumpArticle(id){
  router.push({
    name:'articleDetil',
    query:{
      articleId:id,
    }
  })
}


/** 查询所有文章标签列表*/
function getListAnyArticle(){
  listAnyArticle().then(response => {
    labels.value = response.data;

    if (articleLabelList.value.length === 0){
      for (const item of labels.value) {
        articleLabelList.value.push({ label: item.name, value: item.name });
      }
    }
  });

}

/** 查询文章列表 */
function getList() {
  loading.value = true;
  listArticle(queryParams.value).then(response => {
    articleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  getListAnyArticle()
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    wordNum: null,
    authorId: null,
    author: null,
    likeNum: null,
    evaluate: null,
    see: null,
    isTop: null,
    img: null,
    sendTime: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    labelIds:[]
  };
  proxy.resetForm("articleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  //查询所有文章标签
  //getListAnyArticle();
  reset();
  open.value = true;
  title.value = "添加文章";

}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getArticle(_id).then(response => {
    form.value = response.data;
    //显示所分配了的标签
    form.value.labelIds = response.data.labelIds
    //下拉框显示所有标签
    labels.value = response.data.labels
    open.value = true;
    title.value = "修改文章";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["articleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArticle(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArticle(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除文章编号为"' + _ids + '"的数据项？').then(function() {
    return delArticle(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 发布按钮操作 */
function handlePublish(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认发布文章编号为"' + _ids + '"的数据项？').then(function() {
    row.sendTime = moment().format('YYYY-MM-DD HH:mm:ss');
    row.status = '0'//文章表中的字段已发布
    row.articleStatus = '0'//标签表中的数据
    updateArticle(row).then(response => {
      getList();
      proxy.$modal.msgSuccess("发布成功");
    });
  }).then(() => {

  }).catch(() => {});
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('article/article/export', {
    ...queryParams.value
  }, `article_${new Date().getTime()}.xlsx`)
}

getList();

</script>
<style>
.my-button{
  margin-left: 12px;
}

</style>